/* ServiceServlet.java
 *
 * Project/Product: GW
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Jackey	2008-3-4	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package system.base;

import java.util.Timer;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import system.download.JwxtTimerTask;
import system.imp.DataDownTask;
import framework.core.*;

public class ServiceServlet extends HttpServlet {

    private static final long serialVersionUID = 11411765970400966L;
    
    public static Timer iDownTimer = null;
    public void init(ServletConfig aConfig) throws ServletException {
        super.init(aConfig);
        try {  
            //GWLogWriter.info("===== 初始化定时服务器模块(ServiceServlet) ===== Start");
            System.out.println("===== 初始化定时服务器模块(ServiceServlet) ===== Start");
            //当前时间
            long tNowTime = new DateUtil().getHour() * 60 * 60
            + new DateUtil().getMinute() * 60
            + new DateUtil().getSecond();            
            iDownTimer = new Timer();
//            
//            String sDownTime = Config.getGSDownTime("06:30:00");
//            if (sDownTime == null || sDownTime.length()<8 ){
//                sDownTime = "06:30:00";
//            }
//            //将sDownTime进行了秒转换
//            long lDownTime = Long.parseLong(sDownTime.substring(0, 2)) * 60
//                       * 60 + Long.parseLong(sDownTime.substring(3, 5)) * 60
//                       + Long.parseLong(sDownTime.substring(6, 8));
//            //true:在当前时间延时(lDownTime - tNowTime)秒后运行
//            //false:在当前时间延时(lDownTime - tNowTime)//此一定为负值  加上一天的秒数  后运行
//            long tDownDelay = (lDownTime > tNowTime ? (lDownTime - tNowTime)
//                        : (lDownTime - tNowTime + 24 * 60 * 60)) * 1000;
            
            
            
            
            
            
            String jwxt = "01:00:00";
            long jwxtLong = Long.parseLong(jwxt.substring(0, 2)) * 60
            * 60 + Long.parseLong(jwxt.substring(3, 5)) * 60
            + Long.parseLong(jwxt.substring(6, 8));
            long jwxtDelay;
            if(jwxtLong > tNowTime)
                jwxtDelay = jwxtLong - tNowTime;
            else
                jwxtDelay = (jwxtLong - tNowTime + 24 * 60 * 60) * 1000;
            
            
            
            String storageMonthOut = "02:00:00";
            long storageMonthOutLong = Long.parseLong(storageMonthOut.substring(0, 2)) * 60
            * 60 + Long.parseLong(storageMonthOut.substring(3, 5)) * 60
            + Long.parseLong(storageMonthOut.substring(6, 8));
            long storageMonthOutDelay;
            if(storageMonthOutLong > tNowTime)
                storageMonthOutDelay = storageMonthOutLong - tNowTime;
            else
                storageMonthOutDelay = (storageMonthOutLong - tNowTime + 24 * 60 * 60) * 1000;
//            iDownTimer.scheduleAtFixedRate(new DataDownTask(), tDownDelay, 24 * 60 * 60 * 1000 );
            iDownTimer.scheduleAtFixedRate(new JwxtTimerTask(), jwxtDelay, 24 * 60 * 60 * 1000 );
            iDownTimer.scheduleAtFixedRate(new StorageMonthOutTask(), storageMonthOutDelay, 24 * 60 * 60 * 1000 );
            
            
            //long jwxtDelay = (jwxtLong > tNowTime ? (jwxtLong - tNowTime): (jwxtLong - tNowTime + 24 * 60 * 60)) * 1000;

            
            
            
            
            
            
            
            //GWLogWriter.info("===============下载上海集中采购平台数据定时器启动===============");
        } catch (Exception e) {
            e.printStackTrace();
            //GWLogWriter.fatal("******************[初始化定时服务器模块(ServiceServlet)] 失败********************");
            throw new ServletException(e);
        } catch (Error err) {
            err.printStackTrace();
            //GWLogWriter.fatal("******************[初始化定时服务器模块(ServiceServlet)] 失败********************");
            throw new ServletException(err);
        } finally {
            //GWLogWriter.info("===== 初始化定时服务器模块(ServiceServlet) ===== End");
            System.out.println("===== 初始化定时服务器模块(ServiceServlet) ===== End");
        }
    }
        /**
         * 结束
         */
        public void destroy(){
            //GWLogWriter.info("===== 卸载定时服务器模块(ServiceServlet) ===== Start");
            try {
            }
            catch (Exception e) {
                //GWLogWriter.error("ServiceServlet::destroy() - " + e.getClass().getName() + "[" + e.getMessage() + "]");
            }
            catch (Error e) {
                //GWLogWriter.error("ServiceServlet::destroy() - " + e.getClass().getName() + "[" + e.getMessage() + "]");
            }
            finally {
                //GWLogWriter.info("===== 卸载定时服务器模块(ServiceServlet) ===== End");
                //GWLogWriter.info("");
            }
            super.destroy();
        }        
}
